package com.marry.daodaomanage.ui.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.marry.daodaomanage.infrastructure.service.OAuthService;
import com.marry.daodaomanage.infrastructure.service.dto.UserAccessToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;

/**
 * @author mal
 * @date 2022-11-18 16:16
 */
@RequestMapping("/login")
@RestController
@RefreshScope
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private OAuthService oAuthService;

    @Value("${marry.address:啥}")
    private String address;




    @RequestMapping("/login")
    public UserAccessToken login(String username, String password) {
        String grantType = "password";
        String scope = "user_info";
        String clientId = "daodao";
        String clientSecret = "123";
        String authorization = "Basic " + Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes());
        UserAccessToken token = oAuthService.token(username, password, grantType, scope, null, authorization);
        return token;

    }

    @RequestMapping("authServerHello")
    public String authServerHello() {
        logger.warn("address:{}", address);

        return oAuthService.homeIndex();
    }
}
